package com.ljm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljm.entity.RoleEntity;
import com.ljm.entity.UserEntity;
import com.ljm.entity.UserRoleEntity;
import com.ljm.entity.vo.pathVo;
import com.ljm.mapper.RoleMapper;
import com.ljm.mapper.UserMapper;
import com.ljm.mapper.UserRoleMapper;
import com.ljm.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author song
 * @since 2021-05-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    // 用户
    @Autowired
    UserMapper userMapper;
    /// 角色
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RedisUtil redisUtil;



    //  依据用户ID  得到  角色 +  菜单编码
    @Override
    public List<String> getUserAuthorityInfo(Integer userId) {
        List<String> str=userMapper.getUserAuthorityInfo(userId);
       // baseMapper.selectAllPermissionValue(1);
        return str;
    }

    @Override
    public List<pathVo> getbyname(Integer userid) {
        return userMapper.getbyname(userid);
    }

    @Override
    public String getUserAuthorityMen(String userFromToken) {

        //  ROLE_admin,ROLE_normal,sys:user:list,....
        String authority = "";
        UserEntity user_login = userMapper.selectOne(new QueryWrapper<UserEntity>().eq("user_login", userFromToken));
        Integer userid = user_login.getUserid();
        if (redisUtil.hasKey(userFromToken)) {
            authority = (String) redisUtil.get(userFromToken);
        }else {
            /// 获取角色
            List<UserRoleEntity> userid1 = userRoleMapper.selectList(new QueryWrapper<UserRoleEntity>().eq("userid", userid));
            if (userid1.size() > 0) {
                ArrayList<RoleEntity> objects = new ArrayList<>();
                for (UserRoleEntity userRoleEntity:userid1) {
                    RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleid());
                    objects.add(roleEntity);
                }
                String roleCodes = objects.stream().map(r -> "ROLE_" + r.getRoleName()).collect(Collectors.joining(","));
                authority = roleCodes.concat(",");
            }
            /// 获取对应的菜单权限
            //userMapper.getbynameTwo(userid)
            List<pathVo> getbyname = userMapper.getbynameTwo(userid);
            System.out.println("getbyname = +++"+getbyname);
            if (getbyname.size() > 0) {
                String menuPerms = getbyname.stream().map(m -> m.getPerms()).collect(Collectors.joining(","));

                authority = authority.concat(menuPerms);
            }
        }

        redisUtil.set(userFromToken, authority, 60 * 60);

		return authority;
    }
}
